package com.zixi.beverage_order.service.seller;

import com.zixi.beverage_order.mapper.OrderedProductMapper;
import com.zixi.beverage_order.mapper.ProductMapper;
import com.zixi.beverage_order.pojo.OrderedProduct;
import com.zixi.beverage_order.pojo.Product;
import com.zixi.beverage_order.pojo.ReturnOrderedProductInfo;
import com.zixi.beverage_order.pojo.ReturnProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService{
    @Autowired
    ProductMapper productMapper;
    @Autowired
    OrderedProductMapper orderedProductMapper;

    @Override
    public List<ReturnProductInfo> getAllProduct() {
        Map<String,Object> map = new HashMap<>();
        map.put("deleted",0);
        List<Product> productList = productMapper.selectByMap(map);
        List<ReturnProductInfo> returnProductInfoList = new ArrayList<>();
        for (Product n:productList){
            ReturnProductInfo returnProductInfo = new ReturnProductInfo(String.valueOf(n.getId()), n.getName(), null, n.getRemark(),n.getCategory(),n.getPrice(),n.getPicdata());
            if (n.getCategory()==1){
                returnProductInfo.setCategory("奶茶");
            }else if (n.getCategory()==2){
                returnProductInfo.setCategory("咖啡");
            }
            returnProductInfoList.add(returnProductInfo);
        }
        System.out.println(returnProductInfoList);
        return returnProductInfoList;
    }

    @Override
    public void insertProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public Product selectById(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public void deleteById(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setDeleted(true);
        productMapper.updateById(product);
    }

    @Override
    public List<ReturnOrderedProductInfo> getEveryProductInfo(Long listId) {
        Map<String,Object> map = new HashMap<>();
        map.put("order_list_id",listId);
        List<OrderedProduct> orderedProducts = orderedProductMapper.selectByMap(map);
        List<ReturnOrderedProductInfo> returnOrderedProductInfoList = new ArrayList<>();
        for (OrderedProduct n: orderedProducts){
            ReturnOrderedProductInfo returnOrderedProductInfo = new ReturnOrderedProductInfo();
            //写入产品名
            returnOrderedProductInfo.setProductName(productMapper.selectById(n.getProductId()).getName());
            //写入温度
            if (n.getTemperature() == 2) {
                returnOrderedProductInfo.setTemperature("烫热");
            } else if (n.getTemperature() == 1) {
                returnOrderedProductInfo.setTemperature("温热");
            } else if (n.getTemperature() == 0) {
                returnOrderedProductInfo.setTemperature("常温");
            } else if (n.getTemperature() == -1) {
                returnOrderedProductInfo.setTemperature("去冰");
            } else if (n.getTemperature() == -2) {
                returnOrderedProductInfo.setTemperature("少冰");
            } else if (n.getTemperature() == -3) {
                returnOrderedProductInfo.setTemperature("正常冰");
            }
            if (n.getSweetness() == 0) {
                returnOrderedProductInfo.setSweetness("无糖");
            } else if (n.getSweetness() == 1) {
                returnOrderedProductInfo.setSweetness("微糖");
            } else if (n.getSweetness() == 2) {
                returnOrderedProductInfo.setSweetness("半糖");
            } else if (n.getSweetness() == 3) {
                returnOrderedProductInfo.setSweetness("少糖");
            } else if (n.getSweetness() == 4) {
                returnOrderedProductInfo.setSweetness("全糖");
            }
            returnOrderedProductInfoList.add(returnOrderedProductInfo);
        }

        return returnOrderedProductInfoList;
    }
}
